package com.sky.chapter1.yield;

/**
 * 当前线程让出CPU使用权
 */

public class TestYield implements Runnable {
    TestYield() {
        //创建线程并启动
        Thread t = new Thread(this);
        t.start();
    }

    public static void main(String[] args) {
        new TestYield();
        new TestYield();
        new TestYield();


    }

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            //当i=0时让出CPU执行权，放弃时间片，进行下一轮调度
            if ((i % 5) == 0) {
                System.out.println(Thread.currentThread() + "yield cpu...");
                //当前线程让出CPU执行权，放弃时间片，进行下一轮调度
                Thread.yield();
            }
        }
        System.out.println(Thread.currentThread()+"is over");
    }
}